/**
 * @param {string} type ="Point"|"LineString"|"Circle"|"Polygon"
 * "Square"|"Box"
 *  */
function createDraw({ type = 'Point', source, success }) {
  let geometryFunction = null
  let maxPoints = null
  if (type == 'Box') {
    type = 'LineString'
    maxPoints = 2
    geometryFunction = function (coordinates, geometry) {
      if (!geometry) {
        //多边形
        geometry = new ol.geom.Polygon(null)
      }
      var start = coordinates[0]
      var end = coordinates[1]
      geometry.setCoordinates([
        [start, [start[0], end[1]], end, [end[0], start[1]], start],
      ])
      return geometry
    }
  } else if (type == 'Square') {
    type = 'Circle'
    geometryFunction = ol.interaction.Draw.createRegularPolygon(4)
  }
  let draw = new ol.interaction.Draw({
    type,
    source,
    geometryFunction,
    maxPoints,
  })
  if (typeof success == 'function') {
    /* 返回的就是要素 */
    draw.on('drawend', (evt) => {
      success(evt.feature)
    })
  }
  return draw
}
export default createDraw
